package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.dto.YsLoginRequestDTO;
import edu.ynu.se.xiecheng.achitectureclass.dto.YsLoginResponseDTO;
import edu.ynu.se.xiecheng.achitectureclass.dto.YsCaptureResponseDTO;
import edu.ynu.se.xiecheng.achitectureclass.service.YsService;
import edu.ynu.se.xiecheng.achitectureclass.utils.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/ys")
@CrossOrigin(origins = "http://localhost:3001", allowCredentials = "true", maxAge = 3600)
public class YsController {
    private static final Logger log = LoggerFactory.getLogger(YsController.class);

    @Autowired
    private YsService ysService;

    @PostMapping("/token")
    public ResponseResult<YsLoginResponseDTO> login(@RequestBody YsLoginRequestDTO request) {
        try {
            log.info("获取萤石云token，accountId: {}", request.getAccountId());
            YsLoginResponseDTO response = ysService.login(request.getAccountId());
            log.info("获取萤石云token成功: {}", response);
            return ResponseResult.success(response);
        } catch (Exception e) {
            log.error("获取萤石云token失败: {}", e.getMessage());
            return ResponseResult.error(e.getMessage());
        }
    }

    @PostMapping("/capture")
    public ResponseResult<YsCaptureResponseDTO> capture(
            @RequestParam String accessToken,
            @RequestParam String deviceSerial,
            @RequestParam(defaultValue = "1") String channelNo,
            @RequestParam(required = false) Integer quality) {
        try {
            log.info("接收到抓拍请求：deviceSerial={}, channelNo={}, quality={}", 
                deviceSerial, channelNo, quality);
            
            YsCaptureResponseDTO response = ysService.capture(
                accessToken,
                deviceSerial,
                channelNo,
                quality
            );
            
            return ResponseResult.success(response);
        } catch (Exception e) {
            log.error("抓拍失败：{}", e.getMessage());
            return ResponseResult.error(e.getMessage());
        }
    }
} 